/**
 * 
 */
package com.shengruan.index.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimerTask;

import com.shengruan.admin.jdbc.AuctionHistoryJdbc;
import com.shengruan.admin.jdbc.ProductJdbc;
import com.shengruan.admin.jdbc.UserAccountJdbc;
import com.shengruan.admin.jdbc.UserFavoritesJdbc;
import com.shengruan.admin.vo.AuctionHistory;
import com.shengruan.admin.vo.Product;
import com.shengruan.admin.vo.User;
import com.shengruan.common.Function;
import com.shengruan.user.jdbc.AutoAuctionJdbc;

/**
 * @author liubin
 *
 */
public class AuctionUtil extends TimerTask{


	public static Map<Integer,Product> productMap = new LinkedHashMap<Integer,Product>();

	/***
	 * 更新商品
	 * @param product
	 */
	public static void updatProduct(Product product)
	{
		if(product!=null)
		{
			productMap.put(product.getId(), product);

		}
	}
	/***
	 * 新增商品
	 */
	public static void addProduct(Product product)
	{
		if(product!=null)
		{
			productMap.put(product.getId(), product);
		}
	}

	/**
	 * 删除商品
	 */
	public static void removeProduct(Integer id)
	{
		productMap.remove(id);
	}

	/**
	 * 根据ID获得商品
	 */
	public static Product getById(Integer id)
	{
		return productMap.get(id);
	}

	/***
	 * 根据类别获得商品列表
	 */
	public static List<Product> getList(Integer classId,Integer status)
	{
		List<Product> productList = new ArrayList<Product>();
		Set<Integer> keySet = AuctionUtil.productMap.keySet();
		Iterator<Integer> iterator = keySet.iterator();
		while (iterator.hasNext()) {
			Product product = (Product) AuctionUtil.productMap.get((Integer) iterator.next());
			if (classId == null || classId.intValue() == 0) {
				if(status==0||status==product.getAuctionStatus())
				{
					productList.add(product);
				}
			} else {
				if (product.getClassId() == classId) {
					if(status==0||status==product.getAuctionStatus())
					{
						productList.add(product);
					}
				}
			}
		}
		return productList;
	}


	public void run() 
	{
		checkProduct();
	}



	public  synchronized void checkProduct()
	{
		try
		{

			long nowTime = System.currentTimeMillis();
			List<Product> productList = new ArrayList<Product>();
			if(productMap==null)
			{
				System.out.println("productMap=null....................");
				return;
			}
			Set<Integer> keySet = productMap.keySet();
			Iterator<Integer> iterator = keySet.iterator();
			while(iterator.hasNext())
			{
				Product product = (Product)productMap.get((Integer)iterator.next());
				checkSingleProduct(product,nowTime,productList,1);
			}

			for(int i=0;i<productList.size();i++)
			{
				Product pro = (Product)productList.get(i);
				AuctionUtil.updatProduct(pro);
			}
			productList.clear();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}

	}
	
	
	/**
	 * 监测单个商品
	 */
	public static synchronized void checkSingleProduct(Product product,long nowTime,List<Product> productList,int type)
	{
		if(product.getAuctionStatus()==1)
		{
			if(product.getAuctionBeginTime()<=nowTime)
			{
				ProductJdbc productJdbc = new ProductJdbc();
				product.setAuctionStatus(2);
				productJdbc.updateStatus(product.getId(), 2);
				if(type==1)
					productList.add(product);
			}
		}
		else if(product.getAuctionStatus()==2)
		{
			Long thisAcutionEndTime = product.getThisAuctionEndTime();
			if(thisAcutionEndTime==null)
				thisAcutionEndTime = 0L;
			if(thisAcutionEndTime<=nowTime)
			{
				Long thisBidTime = getBidTime(product);
				product.setThisBidTime(thisBidTime);
				thisAcutionEndTime = nowTime+thisBidTime;
				product.setThisAuctionEndTime(thisAcutionEndTime);
				
				/***
				 * 商品拍卖结束条件
				 */
				if(thisBidTime==0||(thisBidTime==15*1000&&nowTime-product.getLastBidTime()>15*1000))
				{
					ProductJdbc productJdbc = new ProductJdbc();
					product.setAuctionStatus(3);
					productJdbc.updateStatus(product.getId(), 3);
					if(type==1)
						productList.add(product);
					/***拍卖完毕之后  自动移除购物车***/
					UserFavoritesJdbc userFavoritesJdbc = new UserFavoritesJdbc();
					userFavoritesJdbc.deleteByProductId(product.getId());
					/***删除掉自动竞拍记录****/
					AutoAuctionJdbc autoAuctionJdbc = new AutoAuctionJdbc();
					autoAuctionJdbc.deleteByProductId(product.getId());
				}
			}
			if(product.getAuctionBeginTime()>nowTime)
			{
				ProductJdbc productJdbc = new ProductJdbc();
				product.setAuctionStatus(1);
				productJdbc.updateStatus(product.getId(), 1);
			}
			if(type==2)
				AuctionUtil.updatProduct(product);
		}
	}
	/***
	 * 获得每轮的时间
	 */
	public static long getBidTime(Product product)
	{
		Double currentPrice = product.getCurrentPrice();
		Double capPrice = product.getPriceCap();
		Integer auctionTactics = product.getAuctionTactics();
		Integer bidRoundNum = product.getBidRound();
		long lastBidTime = product.getLastBidTime();
		if(currentPrice>=0&&currentPrice<capPrice/3)
		{
			if(bidRoundNum>=1&&System.currentTimeMillis()-lastBidTime>90*1000&&lastBidTime!=0)
				return 0;
			else
				return 90*1000;
		}
		else if(currentPrice>=capPrice/3&&currentPrice<=2*capPrice/3)
		{
			if(bidRoundNum>=1&&System.currentTimeMillis()-lastBidTime>60*1000&&lastBidTime!=0)
				return 0;
			else
				return 60*1000;
		}
		else if(currentPrice>2*capPrice/3&&currentPrice<capPrice)
		{
			if(bidRoundNum>=1&&System.currentTimeMillis()-lastBidTime>30*1000&&lastBidTime!=0)
				return 0;
			else
				return 30*1000;
		}
		else
		{
			if(auctionTactics==0)
			{
				return 0;
			}
			else
			{
				if(bidRoundNum>=1&&System.currentTimeMillis()-lastBidTime>15*1000&&lastBidTime!=0)
					return 0;
				else
					return 15*1000;
			}
		}
	}
	
	/***
	 * 竞拍商品
	 */
	public static synchronized String bidProduct(int id,User user,int autoAuctionId)
	{
		AuctionHistoryJdbc auctionHistoryJdbc = new AuctionHistoryJdbc();
		UserAccountJdbc userAccountJdbc = new UserAccountJdbc();
		ProductJdbc productJdbc = new ProductJdbc();
		Product product = AuctionUtil.getById(id);
		if(product==null)
		{
			return "error";
		}
		if(product.getBidUserId()!=null&&product.getBidUserId()==user.getId())
		{
			
			return "lastUser";
		}
		

		Integer userBidingCount = userAccountJdbc.getBiddingCount(user.getId());
		if(userBidingCount<1)
		{
		
			return "lackBid";
		}
		if(product.getAuctionTactics()==0&&product.getCurrentPrice()>=product.getPriceCap())
		{
			return "error";
		}
		
		if(product.getAuctionStatus()==1||product.getAuctionStatus()==3)
		{
			return "over";
		}
		product.setCurrentPrice(Math.round((product.getCurrentPrice()+0.01)*100)/100.0);
		product.setBidRound(product.getBidRound()+1);
		product.setBidUserName(user.getUserName());
		product.setBidUserId(user.getId());
		Long bidTime = System.currentTimeMillis();
		product.setThisAuctionEndTime(bidTime);
		product.setLastBidTime(bidTime);
		AuctionUtil.updatProduct(product);
		
		/***
		 * 更新用户标数
		 */
		
		userAccountJdbc.updateBiddingCount(user.getId());

		/***
		 * 更新商品
		 */
		productJdbc.updateBid(id, bidTime,user.getId());
		
		/**增加竞拍记录***/
		AuctionHistory auctionHistory = new AuctionHistory();
		auctionHistory.setUserId(user.getId());
		auctionHistory.setProductId(id);
		auctionHistory.setBidPrice(product.getCurrentPrice());
		auctionHistory.setBidRound(product.getBidRound());
		auctionHistory.setTime(System.currentTimeMillis());
		auctionHistory.setIp(user.getLoginIP());
		auctionHistory.setAutoAuctionId(autoAuctionId);
		auctionHistoryJdbc.save(auctionHistory);
		
		long nowTime = System.currentTimeMillis();
		checkSingleProduct(product,nowTime,null,2);
		
		
		return "success";
	}
	
	public static void main(String [] args)
	{
		System.out.println(Function.getLongDate("2010-10-01 18:50:01"));
	}
}
